package com.tan.lbs.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Xml;

import com.tan.lbs.dao.ItemDAO;
import com.tan.lbs.entity.Item;
import com.tan.lbs.entity.Quest;
import com.tan.lbs.util.Constants;
import com.tan.lbs.util.SQLiteDatabaseHelper;

public class ItemDAOImpl implements ItemDAO {
	
	private Context context;

	public ItemDAOImpl(Context context) {
		this.context = context;
	}

	@Override
	public Item readItem(String itemID, String gameID) {
		Item item = null;
		SQLiteDatabase db = null;
		try {
			XmlPullParser parser = Xml.newPullParser();
			parser.setInput(
					context.getResources().getAssets()
							.open("gameFiles/" + gameID + "/item.xml"), "UTF-8");

			int event = parser.getEventType();
			String searchID;
			boolean found = false;
			while (event != XmlPullParser.END_DOCUMENT) {
				if (event == XmlPullParser.START_TAG) {
					if ("itemID".equals(parser.getName())) {
						searchID = parser.nextText();
						if (searchID.equals(itemID)) {
							item = new Item();
							item.setItemID(itemID);
							found = true;
						}
					} else if ("title".equals(parser.getName()) && found) {
						item.setTitle(parser.nextText());
					} else if ("displayText".equals(parser.getName())&& found) {
						item.setDisplayText(parser.nextText());
					} else if ("iconID".equals(parser.getName())&& found) {
						item.setIconID(parser.nextText());
					} else if ("lng".equals(parser.getName())&& found) {
						item.setLng(Float.valueOf(parser.nextText()));
					} else if ("lat".equals(parser.getName())&& found) {
						item.setLat(Float.valueOf(parser.nextText()));
					} else if ("pickable".equals(parser.getName())&& found) {
						item.setPickable(parser.nextText());
					} else if ("containedMediaID".equals(parser.getName())&& found) {
						item.setContainedMediaID(parser.nextText());
					} 
				} else if (event == XmlPullParser.END_TAG && found && "item".equals(parser.getName())) {
					SQLiteDatabaseHelper dbHelper = new SQLiteDatabaseHelper(
							context, Constants.LOCAL_DB_NAME);
					db = dbHelper.getReadableDatabase();
					
					Cursor cursor = db.query("item", new String[] { "itemID",
							"status" }, "itemID=?",
							new String[] { item.getItemID() }, null, null,
							null);
					if(cursor.moveToNext()) {
						item.setStatus(cursor.getString(cursor.getColumnIndex("status")));
					}
					
					break;
				}
				event = parser.next();
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(db!=null) {db.close();}
		}
		return item;
	}

	@Override
	public List<Item> retreiveItems(String gameID) {
		List<Item> items = null;
		Item item = null;
		SQLiteDatabase db = null;
		try {
			XmlPullParser parser = Xml.newPullParser();
			parser.setInput(
					context.getResources().getAssets()
							.open("gameFiles/" + gameID + "/item.xml"), "UTF-8");

			int event = parser.getEventType();
			while (event != XmlPullParser.END_DOCUMENT) {
				switch (event) {
				case XmlPullParser.START_DOCUMENT:
					items = new ArrayList<Item>();
					break;
				case XmlPullParser.START_TAG:
					if ("item".equals(parser.getName())) {
						item = new Item();
					}
					if (item != null) {
						if ("itemID".equals(parser.getName())) {
							item.setItemID(parser.nextText());
						} else if ("title".equals(parser.getName())) {
							item.setTitle(parser.nextText());
						} else if ("displayText".equals(parser.getName())) {
							item.setDisplayText(parser.nextText());
						} else if ("iconID".equals(parser.getName())) {
							item.setIconID(parser.nextText());
						} else if ("lng".equals(parser.getName())) {
							item.setLng(Float.valueOf(parser.nextText()));
						} else if ("lat".equals(parser.getName())) {
							item.setLat(Float.valueOf(parser.nextText()));
						} else if ("pickable".equals(parser.getName())) {
							item.setPickable(parser.nextText());
						} else if ("containedMediaID".equals(parser.getName())) {
							item.setContainedMediaID(parser.nextText());
						} 
					}
					break;
				case XmlPullParser.END_TAG:
					if ("item".equals(parser.getName())) {
						SQLiteDatabaseHelper dbHelper = new SQLiteDatabaseHelper(
								context, Constants.LOCAL_DB_NAME);
						 db = dbHelper.getReadableDatabase();
						
						Cursor cursor = db.query("item", new String[] { "itemID",
								"status" }, "itemID=?",
								new String[] { item.getItemID() }, null, null,
								null);
						if(cursor.moveToNext()) {
							item.setStatus(cursor.getString(cursor.getColumnIndex("status")));
						}
						items.add(item);
						item = null;
					}
					break;
				}
				event = parser.next();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(db!=null) {
				db.close();
			}
		}
		return items;
	}

	@Override
	public void updateItemStatus(String itemID, String status) {
		SQLiteDatabaseHelper dbHelper = new SQLiteDatabaseHelper(
				context, Constants.LOCAL_DB_NAME);
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("status", status);
		db.update("item", values, "itemID=?", new String[]{itemID});
		db.close();
		
	}
	
	@Override
	public String readItemStatus(String itemID) {
		SQLiteDatabaseHelper dbHelper = new SQLiteDatabaseHelper(
				context, Constants.LOCAL_DB_NAME);
		 SQLiteDatabase db = dbHelper.getReadableDatabase();
		
		Cursor cursor = db.query("item", new String[] { "itemID",
				"status" }, "itemID=?",
				new String[] { itemID }, null, null,
				null);
		cursor.moveToNext();
		String statusReturn = cursor.getString(cursor.getColumnIndex("status"));
		db.close();
		return statusReturn;
	}

}
